package com.hspedu.outputStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author LiuJinTao
 * @className FileCopy
 * @date 2024/06/27 16:14
 * @description TODO
 */
public class FileCopy {
    public static void main(String[] args) {
        // 完成文件拷贝
        String srcpath = "e:\\壁纸1.jpg";
        String destpath = "d:\\壁纸1.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcpath);
            fileOutputStream = new FileOutputStream(destpath, true);
            byte[] bytes = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(bytes)) != -1) {
                // 读取到后，就写入到文件
                fileOutputStream.write(bytes, 0, readLen);
            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
